<?php
namespace TwinPeaks\Core;

class CConfig
{
	use TSingleton, TAppInjector;
	
	/**
	 * @var array
	 */
	private $container = [];

	/**
	 * @return void
	 */
	protected function onCreate()
	{
		$this->addVariable('debugMode', true, true);
	}
	
	/**
	 * @return CConfig
	 */
	function addVariable($name, $value, $allowOverride = false)
	{
		if (isset($this->container[$name]) &&
			!$this->container[$name]['allow_override'])
		{
			CException::configCannotOverride($name)->_throw();
		}
		
		$this->container[$name] = [
			'value' => $value,
			'allow_override' => $allowOverride
		];
		
		return $this;
	}
	
	/**
	 * @return mixed
	 */
	function __get($name)
	{
		if (!isset($this->container[$name]))
			return false;
		else
			return $this->container[$name]['value'];
	}
	
	/**
	 * @return void 
	 */
	function __set($name, $value)
	{
		$this->addVariable($name, $value, true);
	}
	
	/**
	 * 
	 */
	function applyFromFile($filename)
	{
		include(self::getApplication()->resolvePath($filename));
	}
}

